<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	public function index()
	{
		
		//$this->load->view('home_view');
	}
	public function user_cat_select() {
		$user_cookie = $this->input->post('user_cookie');
		$cat_id = $this->input->post('cat_id');
		$checked = $this->input->post('checked');
		
		$get_user_id = $this->db->query("SELECT * FROM user WHERE user_cookie = '$user_cookie'");
		$user_id = (int)$get_user_id->row(0)->user_id;
		
		if ($checked == 'true') {
			$query = $this->db->query("INSERT IGNORE INTO `user_cat_select` (`user_id`, `cat_id`) VALUES ('$user_id', '$cat_id');");
		} else {
			$query = $this->db->query("DELETE FROM `user_cat_select` WHERE user_id = '$user_id' AND cat_id = '$cat_id'");
		}
		
		echo $user_id  . " - " . $cat_id . " - " . $checked;
	}
	public function user_cat_view() {
		$user_cookie = $this->input->post('user_cookie');
		$cat_id = (int)$this->input->post('cat_id');
		if ($cat_id != 0) {
			$get_user_id = $this->db->query("SELECT * FROM user WHERE user_cookie = '$user_cookie'");
			$user_id = (int)$get_user_id->row(0)->user_id;
			$this->db->query("INSERT INTO user_cat_view (user_id, cat_id, views) VALUES($user_id, $cat_id, 1) ON DUPLICATE KEY UPDATE views = views + 1");
		}
	}
	
	public function new_rss_feed() {
		$rsslink = $this->db->escape($this->input->post('new_rss_source'));
		
		$source_url = $this->db->escape($this->input->post('select_homepage_url'));
		$source_name = $this->db->escape($this->input->post('select_homepage_name'));
		$source_id = (int)($this->input->post('select_homepage_id'));
		
		$cat_name = $this->db->escape($this->input->post('select_cat_name'));
		$cat_id = (int)($this->input->post('select_cat_id'));
		if ($rsslink == "" || $source_id == 0 || $cat_id == 0) {
			echo "FALSE";
			return;
		}
		
		$ins_cat = $this->db->query("INSERT IGNORE INTO  `category` (  `cat_id` , `cat_name` ) VALUES ( NULL ,  $cat_name )");
		$new_cat_id = (int)$this->db->insert_id();
		if ($new_cat_id == 0) {
			$query = $this->db->query("SELECT * FROM category WHERE cat_name = $cat_name");
			$new_cat_id = (int)$query->row(0)->cat_id;
		}
		
		
		$ins_source = $this->db->query("INSERT IGNORE INTO  `source` (  `source_id` , `source_name` , `source_homepage` ) VALUES ( NULL ,  $source_name,  $source_url )");
		$new_source_id = (int)$this->db->insert_id();
		if ($new_source_id == 0) {
			$query = $this->db->query("SELECT * FROM source WHERE source_name = $source_name AND source_homepage = $source_url");
			$new_source_id = (int)$query->row(0)->source_id;
		}
		//rsslink
		$query = $this->db->query("INSERT INTO  `rsslink` ( `rsslink_id` , `rsslink_url` , `source_id` , `cat_id` ) VALUES ( NULL ,  $rsslink,  $new_source_id,  $new_cat_id );");
		$new_rsslink_id = (int)$this->db->insert_id();
		//echo "Source: " . $new_source_id . " - Cat: " . $new_cat_id . " - RSSLINK: " . $new_rsslink_id;
		echo "OK";
		
	}
}

/* End of file ajax.php */
/* Location: ./application/controllers/ajax.php */